package www.xpu.edu.app.student;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import www.xpu.edu.app.Activity_findjob;
import www.xpu.edu.app.JavaBean;
import www.xpu.edu.app.R;
import www.xpu.edu.app.Work;
import www.xpu.edu.app.WorkAdapter;
import www.xpu.edu.app.WorkBean;

public class Activity_stu_1 extends AppCompatActivity {

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();


        if (actionBar != null) {
            actionBar.hide();
        }
        setContentView(R.layout.activity_stu_1);
        Button button_back=findViewById(R.id.title_back);
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        sendRequest();
        TextView text=(TextView)findViewById(R.id.title_text);
        text.setText("求职");
        SharedPreferences pref=getSharedPreferences("data",MODE_PRIVATE);
        String resume=pref.getString("resume","");
        System.out.println(resume);
        ListView list=findViewById(R.id.stu1_list);
                    /*for (int i = 0; i < 10; i++) {
                        arraylist.add(new Work("管培生","10k-15k","北京","今天"));
                    }*/
        List<WorkBean> list2=new ArrayList<>();
      Gson gson=new Gson();
        list2=gson.fromJson(resume,new TypeToken<List<WorkBean>>(){}.getType());
       // list2=JSON.parseObject(resume,new TypeReference<List<WorkBean>>(){});
        List<Work> arraylist=new ArrayList<>();
        for (int i = 0; i < (list2 != null ? list2.size() : 0); i++) {
            arraylist.add(new Work(list2.get(i).getJobbname(),list2.get(i).getWage(),list2.get(i).getAddress(),list2.get(i).getTime()));
        }
        //重启这个Activity
        if((list2 != null ? list2.size() : 0) ==0)
        {startActivity(new Intent(Activity_stu_1.this,Activity_stu_1.class));
            System.out.println("跳转到本身的Activity,达到刷新的效果");
        }
        WorkAdapter adapter=new WorkAdapter(Activity_stu_1.this,R.layout.work_list_layout,arraylist);

        //list.deferNotifyDataSetChanged();
        list.setAdapter(adapter);


       final List<WorkBean> finalList1 = list2;
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // Toast.makeText(Activity_stu_1.this,String.valueOf(finalList1 != null ? finalList1.get(position).getId() : 0),Toast.LENGTH_LONG).show();
                //Toast.makeText(Activity_stu_1.this,finalList1.get(position).toString(),Toast.LENGTH_LONG).show();
                //构造一个字符集合
                            /*intent.putExtra("id",String.valueOf(finalList1.get(position).getId()));
                            intent.putExtra("time",finalList1.get(position).getTime());
                            intent.putExtra("corname",finalList1.get(position).getCorname());
                            intent.putExtra("type",finalList1.get(position).getType());
                            intent.putExtra("address",finalList1.get(position).getAddress());
                            intent.putExtra("jobname",finalList1.get(position).getJobbname());
                            intent.putExtra("dep",finalList1.get(position).getDep());
                            intent.putExtra("exp",finalList1.get(position).getExp());
                            intent.putExtra("wage",finalList1.get(position).getWage());
                            intent.putExtra("grad",finalList1.get(position).getGrad());
                            intent.putExtra("requ",finalList1.get(position).getRequ());*/
                ArrayList<String> list=new ArrayList<>();
                list.add(String.valueOf(finalList1.get(position).getId()));
                list.add(finalList1.get(position).getTime());
                list.add(finalList1.get(position).getCorname());
                list.add(finalList1.get(position).getType());
                list.add(finalList1.get(position).getAddress());
                list.add(finalList1.get(position).getJobbname());
                list.add(finalList1.get(position).getDep());
                list.add(finalList1.get(position).getExp());
                list.add(finalList1.get(position).getWage());
                list.add(finalList1.get(position).getGrad());
                list.add(finalList1.get(position).getRequ());
                Intent intent=new Intent(Activity_stu_1.this,Activity_stu_2.class);
                intent.putExtra("data",list);
                startActivity(intent);


            }
        });

        // 校园网环境下的本机地址http://10.51.15.101:8080/test20190411/resume
        //View不可见，但占用位置setVisibility(View.INVISIBLE);
        Button button=findViewById(R.id.title_change);
        button.setVisibility(View.INVISIBLE);
        Spinner spinner1=(Spinner)findViewById(R.id.stu1_spinner1);
        Spinner spinner2=(Spinner)findViewById(R.id.stu1_spinner2);
        Spinner spinner3=(Spinner)findViewById(R.id.stu1_spinner3);
        //spinner1:城市
        final String[] city={"请选择城市","北京","西安","天津","深圳","广州","南京","上海","其它"};
        //spinner2:学历
        final String[] grad={"请选择学历","高职大专","普通本科","211","985","硕士","博士及以上","其它"};
        //spinner3:类型
        final String[] type={"请选择工作类型","全职","兼职","实习"};
        ArrayAdapter<String> adapter1=new ArrayAdapter<String>(Activity_stu_1.this,R.layout.support_simple_spinner_dropdown_item,city);
        spinner1.setAdapter(adapter1);
        ArrayAdapter<String> adapter2=new ArrayAdapter<String>(Activity_stu_1.this,R.layout.support_simple_spinner_dropdown_item,grad);
        spinner2.setAdapter(adapter2);
        ArrayAdapter<String> adapter3=new ArrayAdapter<String>(Activity_stu_1.this,R.layout.support_simple_spinner_dropdown_item,type);
        spinner3.setAdapter(adapter3);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               //position的索引从0开始
                if(position!=0)
                Toast.makeText(Activity_stu_1.this,"点击了->"+city[position],Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                Toast.makeText(Activity_stu_1.this,"点击了->"+grad[position],Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                Toast.makeText(Activity_stu_1.this,"点击了->"+type[position],Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }
    private void sendRequest()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder()
                            /*http://192.168.43.188:8080/test2/resume*/
                            /*http://192.168.123.196:8080/test2/resume*/
                            //.url("http://192.168.43.188:8080/test2/resume")
                            .url("http://188.131.250.4:8080/xpu/resume")
                            .build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
                    SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
                    editor.putString("resume",responseData);
                    editor.apply();




                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
}
